package com.cszjzx.member.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.cszjzx.member.service.*;
import com.cszjzx.member.po.*;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class LoginCheckAction extends ActionSupport {

	private String name;
	private String password;
	private int module;
	private List<Stage> stages;
	private List<Field> fields;
	private int audit,auditPass,auditUnpass,edit;
	StageService stageService = new StageServiceImpl();
	FieldService fieldService = new FieldServiceImpl();
	MasterService masterservice = new MasterServiceImpl();
	HttpServletRequest request = ServletActionContext.getRequest();
	TempleMemberService templeMemberService = new TempleMemberServiceImpl();
	
	public int getEdit() {
		return edit;
	}
	public void setEdit(int edit) {
		this.edit = edit;
	}
	public int getAudit() {
		return audit;
	}
	public void setAudit(int audit) {
		this.audit = audit;
	}
	public int getAuditPass() {
		return auditPass;
	}
	public void setAuditPass(int auditPass) {
		this.auditPass = auditPass;
	}
	public int getAuditUnpass() {
		return auditUnpass;
	}
	public void setAuditUnpass(int auditUnpass) {
		this.auditUnpass = auditUnpass;
	}
	public List<Stage> getStages() {
		return stages;
	}
	public void setStages(List<Stage> stages) {
		this.stages = stages;
	}
	public List<Field> getFields() {
		return fields;
	}
	public void setFields(List<Field> fields) {
		this.fields = fields;
	}
	public int getModule() {
		return module;
	}
	public void setModule(int module) {
		this.module = module;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * 登录验证
	 * 输入数据的合法性在前台进行验证，比如是否为空等
	 */
	public String execute() throws Exception{
		
		String ret = "login";
		Master master = masterservice.getMasterById(name,module);		//name为masterid
		if(master!=null && password.equals(master.getPassword())){
			request.getSession().setAttribute("master", master);
			ret = "module"+module;
		}else{
			request.setAttribute("message", "用户账号或密码错误！");
		}
		if(module==1){
			stages = stageService.getStageList();
			fields = fieldService.getFieldList();
			request.getSession().setAttribute("stages", stages);
			request.getSession().setAttribute("fields", fields);
			if(master.isIssuper()){
				audit = templeMemberService.getByCheckcondition(1);
				auditPass = templeMemberService.getByCheckcondition(2);
				auditUnpass = templeMemberService.getByCheckcondition(3);
			}
			else{
				int masterid = master.getId();
				edit = templeMemberService.getByMasteridAndCheckcondition(masterid, 0);
				audit = templeMemberService.getByMasteridAndCheckcondition(masterid, 1);
				auditPass = templeMemberService.getByMasteridAndCheckcondition(masterid, 2);
				auditUnpass = templeMemberService.getByMasteridAndCheckcondition(masterid, 3);
				request.getSession().setAttribute("edit", edit);
			}
			request.getSession().setAttribute("audit", audit);
			request.getSession().setAttribute("auditPass", auditPass);
			request.getSession().setAttribute("auditUnpass", auditUnpass);
		}
		return ret;
	}
}
